<?php
/**
 * @author barry
 * dbProxy : true 表示遵从db config的主从配置
 * dbProxy : false 表示强制读写主库
 */
class UserDB{
	
	private $_objDb = null;
	
	private $_objTable = 'tbl_user';
	
	private $_objDbName = 'TEZIGN';
	
	public function __construct($dbProxy = true) {
	   $this->_objDb = core::db()->getConnect($this->_objDbName, $dbProxy);
	}

	/**
	 * 获取单个
	 * @param array $arrAttr
	 * @param string $where
	 */
	public function getOne($arrAttr, $where) {
		$data = array();
		if (!empty($arrAttr)) {
			$strAttr = implode(',', $arrAttr);
		} else {
			$strAttr = '*';
		}
		$strSql = 'select ' . $strAttr . ' from ' . $this->_objTable . ' where ' . $where . ' limit 1';
		$result = $this->_objDb->query($strSql);
		$data = $this->_objDb->fetchArray($result);
		return $data;
	} 
	/**
	 * 获取接口列表
	 * @param array $arrAttr
	 * @param string $where
	 */
	public function getList($arrAttr, $where) {
		$data = array();
		if (!empty($arrAttr)) {
			$strAttr = implode(',', $arrAttr);
		} else {
			$strAttr = '*';
		}
		$strSql = 'select ' . $strAttr . ' from ' . $this->_objTable . ' where ' . $where;
		$result = $this->_objDb->query($strSql);
		while ($arrdataTemp = $this->_objDb->fetchArray($result)) {
			$data[] = $arrdataTemp;
		}
		return $data;
	}
	
	/**
	 * 获取接口列表带分页
	 * @param array $arrAttr
	 * @param string $strWhere
	 * @param int $intPage
	 * @param int $intPageSize
	 * 
	 * return array
	 */
	public function getListWithPage($arrAttr, $strWhere, $intPage, $intPageSize) {
		if (!empty($arrAttr)) {
			$strAttr = implode(',', $arrAttr);
		} else {
			$strAttr = '*';
		}
		$strSql = 'select ' . $strAttr . ' from ' . $this->_objTable . ' where ' . $strWhere . ' limit ' . (($intPage-1) * $intPageSize) . ',' . $intPageSize;
		$result = $this->_objDb->query($strSql);
		while ($arrdataTemp = $this->_objDb->fetchArray($result)) {
			$data['data'][] = $arrdataTemp;
		}
		$data['count'] = $this->_getCount($strWhere);
		return $data;
	}
	
	/**
	 * 更新接口
	 * @param array $arrParam
	 * @param string $where
	 * 
	 * return bool
	 */
	public function update($arrParam, $where) {
		foreach($arrParam as $key => $value) {
			$attr[] = $key . "='" . $value . "'";
		}
		$strSql = 'update ' . $this->_objTable . ' set ' . implode(',', $attr) . ' where ' . $where;
		return $this->_objDb->query($strSql);
	}
	
	/**
	 * 添加接口
	 * @param array $arrParam
	 * 
	 * return int
	 */
	public function add($arrParam) {
		$attrKey = array_keys($arrParam);
		foreach($arrParam as $key => $value) {
			$attrValues[] ="'" . $value . "'";
		}
		$strSql = 'insert into ' . $this->_objTable . '(' . implode(",", $attrKey) . ') values (' . implode(',', $attrValues) . ')';
		$result = $this->_objDb->query($strSql);
		if ($result) {
			$intId = $this->_objDb->insertId();
		} else {
			$intId = 0;
		}
		return $intId;
	}
	
	/**
	 * 删除指定接口
	 * @param string $where
	 * 
	 * return int
	 */
	public function delete($where) {
		$strSql = 'delete from ' . $this->_objTable . ' where ' . $where;
		return $this->_objDb->query($strSql);
	}
	
	/**
	 * 事务开启
	 */
	public function begin() {
	    return $this->_objDb->query('BEGIN');
	}
	
	/**
	 * 事务回滚
	 */
	public function rollback() {
	    return $this->_objDb->query('ROLLBACK');
	}
	
	/**
	 * 事务提交
	 */
	public function commit() {
	    return $this->_objDb->query('COMMIT');
	}
	
	/**
	 * 获取指定sql的查询总数
	 * @param unknown_type $strWhere
	 */
	private function _getCount($strWhere) {
	    $strSql = 'select count(*) as count from ' . $this->_objTable . ' where ' . $strWhere;
	    $result = $this->_objDb->query($strSql);
	    $arrdataTemp = $this->_objDb->fetchArray($result);
	    $count = $arrdataTemp['count'];
	    return $count;
	}
}